<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click.native="addRole"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table v-loading="loading" :data="data">
              <el-table-column
                label="序号"
                width="100"
                :index="indexMethod"
                type="index"
              />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="small"
                    type="success"
                    @click="openRoleDialog(scope.row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click.native="editRole(scope.row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click.native="delRole(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!--//! 分页器-->
            <el-pagination
              :current-page="form.page"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="form.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>

          <!-- //! 公司信息 -->
          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyForm.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyForm.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyForm.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyForm.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- //! 添加弹框 -->
      <add-dialog ref="AddDialog" @updata="initDate" />
      <!-- 分配权限的弹层 -->
      <el-dialog
        title="分配权限"
        :visible="showAssignDialog"
        :close-on-click-modal="false"
        @open="getPermissionList"
        @close="closeAssignDialog"
      >
        <el-tree
          ref="tree"
          v-loading="isTreeLoading"
          :data="permissionList"
          :props="{ label: 'name' }"
          show-checkbox
          :check-strictly="true"
          :default-expand-all="true"
          node-key="id"
        />
        <template #footer>
          <div style="text-align: right">
            <el-button @click="closeAssignDialog">取消</el-button>
            <el-button type="primary" @click="confirmPermissionAssign">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleListApi, delRoleApi, getRoleDetailApi, AssignPermissionApi } from '@/api/setting'
import AddDialog from './components/Dialog.vue'
import { getCompanyApi } from '@/api/company'
import { getPermissionListApi } from '@/api/permission'
import { tileListToTreeData } from '@/utils/index'
export default {
  name: 'Setting',
  components: {
    AddDialog
  },
  data() {
    return {
      // 准备变量接受数据
      data: [],
      // 定义请求参数
      form: {
        page: 1,
        pagesize: 10
      },
      // 总数
      total: null,
      loading: false,
      // 公司回显
      companyForm: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      // 分配权限弹框显示隐藏
      showAssignDialog: false,
      // 分配权限ID
      roleId: '',
      // 权限列表数据
      permissionList: [],
      // 分配权限回显
      permIds: [],
      isTreeLoading: false
    }
  },
  created() {
    this.initDate()
    this.getCompany()
  },
  methods: {
    //! 修改分配权限
    async confirmPermissionAssign() {
      // 获取修改权限数据 => elementUI方法
      const permIdsList = this.$refs.tree.getCheckedKeys()
      // 发送请求
      await AssignPermissionApi({
        id: this.roleId,
        permIds: permIdsList
      })
      this.$message.success('分配成功')
      this.closeAssignDialog()
    },
    //! 获取权限列表
    async getPermissionList() {
      const { data: res } = await getPermissionListApi()
      this.permissionList = tileListToTreeData(res, '0')
    },
    //! 打开分配权限弹框
    async openRoleDialog(id) {
      this.showAssignDialog = true
      // 打开弹框开启loading
      this.isTreeLoading = true
      this.roleId = id
      // 获取权限详情信息
      const { data: { permIds }} = await getRoleDetailApi(id)
      // permIds 点击角色所拥有的权限
      this.permIds = permIds
      // 通过ref和key设置回显 => element-ui
      this.$refs.tree.setCheckedKeys(permIds)
      this.isTreeLoading = false
    },
    //! 关闭分配权限弹框
    closeAssignDialog() {
      this.showAssignDialog = false
    },
    //! 获取数据，初始化列表
    async initDate() {
      this.loading = true
      const res = await getRoleListApi(this.form)
      // console.log(res)
      this.data = res.data.rows
      this.total = res.data.total
      this.loading = false
    },
    //! 每页显示多少个
    handleSizeChange(val) {
      this.form.pagesize = val
      this.form.page = 1
      this.initDate()
    },
    //! 跳转到相应的页面
    handleCurrentChange(val) {
      // console.log(val)
      this.form.page = val
      this.initDate()
    },
    //! 删除角色
    async delRole(id) {
      try {
        await this.$confirm(this.$t('deleteDialog.tips'), this.$t('deleteDialog.tipsTitle'))
      } catch (error) {
        return console.log(error)
      }
      await delRoleApi(id)
      this.$message.success('删除成功')
      // 判断当前页的数量是否只有一条，如果是那么再次点击的时候就让他 page --
      if (this.data.length === 1 && this.form.page > 1) {
        this.form.page--
      }
      this.initDate()
    },
    // !添加角色
    addRole() {
      this.$refs.AddDialog.showDialog = true
    },
    //! 列表序号
    indexMethod(index) {
      return this.form.pagesize * (this.form.page - 1) + index + 1
    },
    // !编辑角色
    editRole(id) {
      this.$refs.AddDialog.getRoleDetail(id)
    },
    // !公司回显
    async getCompany() {
      const companyId = this.$store.state.user.userInfo.companyId
      const res = await getCompanyApi(companyId)
      this.companyForm = res.data
    }
  }
}
</script>

<style>
</style>
